package com.softeem.springboot01.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.springboot01.dto.Result;
import com.softeem.springboot01.entity.User;
import com.softeem.springboot01.mapper.UserMapper;
import com.softeem.springboot01.service.UserService;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Override
    public Result login(String username, String password) {
        //根据用户名查询用户对象
        User user = getBaseMapper().selectOne(Wrappers.query(User.class).eq("username", username));
        if(Objects.isNull(user)){
            return Result.fail().setMsg("账号不存在");
        }
        if(Objects.equals(user.getUsername(),username)){
            return Result.ok().setMsg("登录成功").setData(user);
        }
        return Result.fail().setMsg("密码错误");
    }

    @Override
    public Result reg(String username, String password) {
        User user = getBaseMapper().selectOne(Wrappers.query(User.class).eq("username", username));
        if(Objects.nonNull(user)){
            return Result.fail().setMsg("该账号已存在");
        }
        user = new User();
        user.setUsername(username);
        user.setPassword(password);
        boolean b = save(user);
        if(b){
            return Result.ok().setMsg("注册成功").setData(user);
        }
        return Result.fail().setMsg("注册失败");
    }
}
